import requests

# 课程Id
# 低幼儿童文学名著导读 1589
# 幼儿园组织与管理 1588
# 马克思主义基本原理概论 1260
# 中国近现代史纲要 1261
# 学前儿基心理健康与辅导 1590
# 幼儿区班级管理 1298
LESSON_ID = 1298


def get_lesson_hour():
    url = "https://nbv.iwdjy.com/api/lesson/getLessonHour"
    body = {
        "id": LESSON_ID,
        "token": "ROd0g2ATHMHqWUfOazakoK7M4IAvTKs9XumhMv1M4xINW",
        "url": f"https://www.iwdjy.com/course-detail?id={LESSON_ID}",
        "host": "www.iwdjy.com"
    }

    headers = {
        "Content-Type": "application/json"
    }

    response = requests.post(url, json=body, headers=headers)

    if response.status_code == 200:
        return response.json()
    else:
        response.raise_for_status()


def process_lesson_hour_data():
    try:
        result = get_lesson_hour()
        if 'data' in result and 'data' in result['data']:
            data_array = result['data']['data']
            for item in data_array:
                hourId = item['id']
                save_user_house(hourId)
            return data_array
        else:
            print("返回数据中不包含预期的data字段")
            return None
    except requests.exceptions.RequestException as e:
        print(f"Request failed: {e}")
        return None


def save_user_house(hourId):
    url = "https://nbv.iwdjy.com/api/lesson/saveUserHour"
    body = {
        "hour_id": hourId,
        "play_time": 3500.000888,
        "face_url": "",
        "token": "ROd0g2ATHMHqWUfOazakoK7M4IAvTKs9XumhMv1M4xINW",
        "url": f"https://www.iwdjy.com/course-learn?id={LESSON_ID}&learnId={hourId}&teacherId=0",
        "host": "www.iwdjy.com"
    }

    headers = {
        "Content-Type": "application/json"
    }

    response = requests.post(url, json=body, headers=headers)

    if response.status_code == 200:
        print(response.json())
    else:
        response.raise_for_status()


# 调用函数并打印返回数据
if __name__ == "__main__":
    try:
        process_lesson_hour_data()
        print("执行完成!")
    except requests.exceptions.RequestException as e:
        print(f"Request failed: {e}")
